package UserSystem;

import StudentSystem.StudentTest;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class UserTest {
    static ArrayList<User> list = new ArrayList<>();
    static {
        list.add(new User("zhangdeming","zdm20041214","130429200412146517","1378503264"));
    }
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);
        //欢迎界面
        System.out.println("欢迎来到学生管理系统");
        loop :while (true) {
            System.out.println("1登录 2注册 3忘记密码 4退出");
            System.out.println("请选择:");
            //进行选择
            String choose = sc.next();
            switch (choose){
                case "1"-> log(list);
                //登录
                case "2"-> register(list);
                //注册
                case "3"-> change(list);
                //修改密码
                case "4" -> {
                    break loop;
                    //跳出while循环，退出系统
                }
                //除了1，2，3，4的其他选择无意义
                default -> System.out.println("该选项不存在");
            }
        }
    }
    public static void log(ArrayList<User> list){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你的用户名：");
        String username = sc.next();
        //输入用户名
        int index = getIndex(list, username);
        //寻找username在list里面的索引
        if(index == -1){
            //如果索引是-1，代表不存在这个username
            System.out.println("用户名未注册，请先进行注册");
            return;
        }
        User user = list.get(getIndex(list,username));
        for (int i = 0; i < 3; i++) {
            //一共有三次输入密码的机会，输入错误，则退出系统
            System.out.println("请输入密码");
            String password = sc.next();
            if(user.getPassword().equals(password)){
                while (true) {
                    String code = Code();
                    //Code为随机验证码功能
                    System.out.println("验证码为：" + code);
                    System.out.println("请输入验证码");
                    //验证码输入
                    String code1 = sc.next();
                    if(code1.equalsIgnoreCase(code)){
                        //验证码正确就跳出循环
                        break;
                    }else {
                        //验证码错误就重新生成新的验证码，继续验证
                        System.out.println("验证码输入错误，请重新验证");
                    }
                }
                //获取密码，如果与输入密码相同，则进入系统
                System.out.println("登陆成功，现在你可以使用该管理系统了！！");
                StudentTest ss = new StudentTest();
                ss.startStudentSystem();
                return;
            }else {
                //密码错误就重新输入
                System.out.println("密码错误，你还有" + (2 - i) + "次机会");
            }
        }
        System.out.println("你的账户已经被锁定，请修改密码");
    }//登录方法
    public static void register(ArrayList<User> list){//注册方法
        User user = new User();
        Scanner sc = new Scanner(System.in);
        while (true) {
            //用户名
            System.out.println("请输入你要注册的用户名");
            String username = sc.next();
            if(!checkUsername(list,username)){
                //查询要注册的用户名是否正确，错误的话，重新输入
                System.out.println("你输入的用户名有误，请重新输入");
            }else {
                //正确的时候就将该用户名传入
                user.setUsername(username);
                break;
            }
        }
        while (true) {
            //密码
            System.out.println("请输入注册密码");
            String password1 = sc.next();
            System.out.println("请再次确认输入密码");
            String password2 = sc.next();
            if(password1.equals(password2)){
                //比较两次密码是否相同，相同的话就传入密码
                user.setPassword(password1);
                break;
            }else {
                //不一致的话重新输入
                System.out.println("输入的密码与第一次的不一致，请重新输入");
            }
        }
        while (true){//录入身份证
            System.out.println("请输入你的身份证号码");
            String id = sc.next();
            if(checkId(id)){
                //判断身份证是否正确
                user.setId(id);
                //正确的话就录入
                break;
            }
            else {
                //错误的话就重新输入
                System.out.println("你输入的身份证错误，请重新输入");
            }
        }
        while (true){
            //录入手机号
            System.out.println("请输入你的手机号");
            String tel = sc.next();
            if(checkTel(tel)){//判断手机号
                user.setTel(tel);
                //正确的话录入
                break;
            }else {
                //错误的话重新输入
                System.out.println("你输入的手机号码有误，请重新输入");
            }
        }
        list.add(user);
        //将user加入list里面
        System.out.println("您的用户注册成功");
        visit(list);
    }
    public static boolean checkUsername(ArrayList<User> list,String username){
        //检查用户名是否正确的方法
        int index = getIndex(list, username);
        if(index >= 0) return false;
        //检查是否已经有该用户名
        else if (username.length() < 3 || username.length() > 15) return false;
        //检查长度
        else return check1(username);
    }
    public static int getIndex(ArrayList<User> list,String username){
        //查找用户名所在的索引
        for (int i = 0; i < list.size(); i++) {
            if(list.get(i).getUsername().equals(username)){
                return i;
                //查找完成返回索引
            }
        }
        return -1;
        //没有查找到就返回-1
    }
    public static boolean check1(String username){
        //检查用户名是否是字母加数字或者只有字母，不可有只是数字
        int sum = 0;
        for (int i = 0; i < username.length(); i++) {
            char c = username.charAt(i);
            if((c < '0' || c > '9') && (c < 'a' || c >'z') && (c < 'A' || c > 'Z')){
                return false;
            }
            if (c >= 'A'){
                sum++;
                //判断字母的数量
            }
        }
        return sum != 0;
    }
    public static boolean checkId(String id){
        //查看身份证的长度和第一个数字不能是0
        if(id.length() != 18) return false;
        else if (id.charAt(0) == '0') return false;
        else return check2(id);//引用check2的方法
    }
    public static boolean check2(String id){
        for (int i = 0; i < id.length() - 1; i++) {
            char c = id.charAt(i);
            if(c < '0' || c > '9') return false;
        }
        //检查前17位是否都为数字
        char c = id.charAt(id.length() - 1);
        if(c < '0' || c >'9') {
            return c == 'x' || c == 'X';
        }
        //第18位可以是数字，x，X
        return true;
    }
    public static boolean checkTel(String tel){
        if(tel.length() != 11)return false;
        //电话号必须是11位
        for (int i = 0; i < tel.length(); i++) {
            char c = tel.charAt(i);
            if(c < '0' || c > '9'){
                return false;
                //11位都必须是数字
            }
        }
        return true;
    }
    public static String Code(){
        //随机生成验证码的方法
        Random r = new Random();
        StringBuilder stringBuilder = new StringBuilder();
        String []a = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C"
                ,"D","E", "F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
        for (int i = 0; i < 4; i++){
            int num = r.nextInt(52);
            stringBuilder.append(a[num]);
        }
        String string = stringBuilder.toString();
        //System.out.println(string);
        int num = r.nextInt(10);
        char num1 = (char) (num + '0');
        //System.out.println(num1);
        char []arr = string.toCharArray();
        char []arr1 = new char[5];
        for (int i = 0; i < arr.length; i++) {
            arr1[i] = arr[i];
            //赋值给长度更大的数组
        }
        int change = r.nextInt(5);
        //随机找一个位置给数字插入
        for (int i = arr1.length - 1; i > change; i--) {
            arr1[i] = arr1[i - 1];
            //将数字后的都往后移
        }
        arr1[change] = num1;
        return new String(arr1);
    }
    public static void change(ArrayList<User> list){
        //修改密码方法
        System.out.println("请输入你要修改密码的用户名");
        Scanner sc = new Scanner(System.in);
        String username = sc.next();
        if(getIndex(list,username) == -1){
            //查找用户名是否存在
            System.out.println("该用户未注册，请先注册");
            return;
        }
        User user = list.get(getIndex(list,username));
        System.out.println("请输入你的身份证和手机号码");
        String id = sc.next();
        String tel = sc.next();
        if(user.getId().equals(id) && user.getTel().equals(tel)){
            //验证身份
            System.out.print("你通过了验证");
            while (true) {
                System.out.println("请输入密码");
                String newPassword1 = sc.next();
                System.out.println("请再次输入密码");
                String newPassword2 = sc.next();
                if(newPassword1.equals(newPassword2)){
                    user.setPassword(newPassword1);
                    break;
                }
                else {
                    System.out.println("两次输入密码不一致，请重新输入");
                }
            }
        }else {
            System.out.println("你输入的身份证或密码有误，修改失败");
        }
    }
    public static void visit(ArrayList<User> list){
        for (User user : list) {
            System.out.println(user.getUsername() + "  " + user.getPassword() + "  " + user.getId() + "  " + user.getTel());
        }
    }
}
